package com.sphd.emanu.fragment.time;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.daydetail.DayDetailActivity;
import com.sphd.emanu.activity.moneymessage.MoneyMessageActivity;
import com.sphd.emanu.adapter.TimeFragmentAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.fragment.month.MonthFragment;
import com.sphd.emanu.fragment.search.SearchFragment;
import com.sphd.emanu.interfaces.Letter;
import com.sphd.emanu.mvp.MVPBaseFragment;
import com.sphd.emanu.pojo.bank.TimeShowMoneyMessage;
import com.sphd.emanu.pojo.login.User;
import com.wzutils.L;
import com.wzutils.TimeUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @Author Hu
 * @Time 2017/7/20 -- 15:53.
 * @Email 87683202@qq.com
 */

public class TimeFragment extends MVPBaseFragment<TimeContract.View, TimePresenter> implements TimeContract.View {

    Unbinder unbinder;
    TimeFragmentAdapter timeFragmentAdapter;
    Letter letter;
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    Bundle bundle;
    List<TimeShowMoneyMessage.AccountPeriodListByDateBean> listByDateBeen;
    int state;
    int accid = 0;//当从账户查看跳过来时
    User user;
    Intent intent;
    String beginDate, endDate;
    boolean isOnResum;

    public void setLetter(Letter letter) {
        this.letter = letter;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.myswf_and_recycler;
    }

    @Override
    protected void initView() {
        unbinder = ButterKnife.bind(this, rootView);
        intent = new Intent();
        bundle = getArguments();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        initEvent();
        setRefresh(mySwf);
        initAdapter();
        letter.readyed();
    }

    private void initEvent() {
        mySwf.setOnRefreshListener(() -> addData());
        myRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                TimeShowMoneyMessage.AccountPeriodListByDateBean byDateBean =
                        (TimeShowMoneyMessage.AccountPeriodListByDateBean) adapter.getItem(position);
                bundle.putSerializable(Constant.LONG_NEAM_BEAN, byDateBean);
                bundle.putString(Constant.FLAG_NAME, TimeFragment.class.getName());
                //判断是从月fragment跳转过来的还是搜索fragment跳转过来的 如果是月那就将id 初始化为0
                if (bundle.getString(Constant.CLASS_NAME).equals(MonthFragment.class.getName())) {
                    // TODO: 2017/7/21  
                    if (isOnResum) {
                        bundle.putInt(Constant.ID, 0);
                    }
                    if (state == 1 || state == 2) {
                        bundle.putString(Constant.Salary_User_Start, TimeUtils.dateToString(TimeUtils.stringToDate(byDateBean.getCreateDate(), TimeUtils.
                                YEAR_MONTH_DAY), TimeUtils.YEAR_MONTH_DAY));
                        bundle.putString(Constant.Salary_User_End, TimeUtils.dateToString(TimeUtils.stringToDate(byDateBean.getCreateDate(), TimeUtils.
                                YEAR_MONTH_DAY), TimeUtils.YEAR_MONTH_DAY));
                    } else if (state == 3) {
                        if (position == 0) {
                            bundle.putString(Constant.Salary_User_Start, TimeUtils.dateToString(TimeUtils.stringToDate(byDateBean.getCreateDate(), TimeUtils.
                                    YEAR_MONTH_DAY), TimeUtils.YEAR_MONTH_DAY));
                            bundle.putString(Constant.Salary_User_End, endDate);
                        } else {
                            try {
                                bundle.putString(Constant.Salary_User_Start, TimeUtils.getNowMonthFirstLastday(Calendar.getInstance(),
                                        TimeUtils.stringTolong(byDateBean.getCreateDate(), TimeUtils.YEAR_MONTH_DAY))[0]);
                                bundle.putString(Constant.Salary_User_End, TimeUtils.getNowMonthFirstLastday(Calendar.getInstance(),
                                        TimeUtils.stringTolong(byDateBean.getCreateDate(), TimeUtils.YEAR_MONTH_DAY))[1]);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                        }
                    } else {
                        //staste ==4

                        if (byDateBean.getShowType() == 3) {
                            bundle.putString(Constant.Salary_User_Start, TimeUtils.dateToString(TimeUtils.stringToDate(byDateBean.getCreateDate(), TimeUtils.
                                    YEAR_MONTH_DAY), TimeUtils.YEAR_MONTH_DAY));
                            bundle.putString(Constant.Salary_User_End, TimeUtils.dateToString(TimeUtils.stringToDate(byDateBean.getCreateDate(), TimeUtils.
                                    YEAR_MONTH_DAY), TimeUtils.YEAR_MONTH_DAY));
                        } else if (byDateBean.getShowType() == 2) {

                            if (position == 0) {
                                try {
                                    bundle.putString(Constant.Salary_User_Start, TimeUtils.getNowMonthFirstLastday(Calendar.getInstance(),
                                            TimeUtils.stringTolong(endDate, TimeUtils.YEAR_MONTH_DAY))[0]);
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
                                bundle.putString(Constant.Salary_User_End, endDate);
                            } else {
                                try {
                                    bundle.putString(Constant.Salary_User_Start, TimeUtils.getNowMonthFirstLastday(Calendar.getInstance(),
                                            TimeUtils.stringTolong(endDate, TimeUtils.YEAR_MONTH_DAY))[0]);
                                    bundle.putString(Constant.Salary_User_End, TimeUtils.getNowMonthFirstLastday(Calendar.getInstance(),
                                            TimeUtils.stringTolong(endDate, TimeUtils.YEAR_MONTH_DAY))[1]);
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                } else if (bundle.getString(Constant.CLASS_NAME).equals(SearchFragment.class.getName())) {

                    if (byDateBean.getShowType() == 1) {
                        if (position == 0) {
                            bundle.putString(Constant.Salary_User_Start, TimeUtils.getYearFirstLastDay(Calendar.getInstance(),
                                    TimeUtils.stringTolong(endDate, TimeUtils.YEAR_MONTH_DAY))[0]);
                            bundle.putString(Constant.Salary_User_End, endDate);
                        } else if (position == (adapter.getData().size() - 1)) {
                            bundle.putString(Constant.Salary_User_Start, beginDate);
                            bundle.putString(Constant.Salary_User_End, TimeUtils.getYearFirstLastDay(Calendar.getInstance(),
                                    TimeUtils.stringTolong(beginDate, TimeUtils.YEAR_MONTH_DAY))[1]);
                        } else {
                            bundle.putString(Constant.Salary_User_Start, TimeUtils.getYearFirstLastDay(Calendar.getInstance(),
                                    TimeUtils.stringTolong(byDateBean.getCreateDate(), TimeUtils.YEAR_MONTH_DAY))[0]);
                            bundle.putString(Constant.Salary_User_End, TimeUtils.getYearFirstLastDay(Calendar.getInstance(),
                                    TimeUtils.stringTolong(byDateBean.getCreateDate(), TimeUtils.YEAR_MONTH_DAY))[1]);
                        }
                    } else if (byDateBean.getShowType() == 2) {
                        if (position == 0) {
                            try {
                                bundle.putString(Constant.Salary_User_Start, TimeUtils.getNowMonthFirstLastday(Calendar.getInstance(),
                                        TimeUtils.stringTolong(endDate, TimeUtils.YEAR_MONTH_DAY))[0]);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            bundle.putString(Constant.Salary_User_End, endDate);
                        } else if (position == (adapter.getData().size() - 1)) {
                            bundle.putString(Constant.Salary_User_Start, beginDate);
                            try {
                                bundle.putString(Constant.Salary_User_End, TimeUtils.getNowMonthFirstLastday(Calendar.getInstance(),
                                        TimeUtils.stringTolong(beginDate, TimeUtils.YEAR_MONTH_DAY))[1]);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                        } else {
                            try {
                                bundle.putString(Constant.Salary_User_Start, TimeUtils.getNowMonthFirstLastday(Calendar.getInstance(),
                                        TimeUtils.stringTolong(byDateBean.getCreateDate(), TimeUtils.YEAR_MONTH_DAY))[0]);
                                bundle.putString(Constant.Salary_User_End, TimeUtils.getNowMonthFirstLastday(Calendar.getInstance(),
                                        TimeUtils.stringTolong(byDateBean.getCreateDate(), TimeUtils.YEAR_MONTH_DAY))[1]);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }

                        }
                    } else if (byDateBean.getShowType() == 3) {
                        bundle.putString(Constant.Salary_User_Start, TimeUtils.dateToString(TimeUtils.stringToDate(byDateBean.getCreateDate(), TimeUtils.
                                YEAR_MONTH_DAY), TimeUtils.YEAR_MONTH_DAY));
                        bundle.putString(Constant.Salary_User_End, TimeUtils.dateToString(TimeUtils.stringToDate(byDateBean.getCreateDate(), TimeUtils.
                                YEAR_MONTH_DAY), TimeUtils.YEAR_MONTH_DAY));
                    }
                } else {
                    String[] firstAend = initTwoTime(byDateBean);
                    bundle.putString(Constant.Salary_User_Start, firstAend[0]);
                    bundle.putString(Constant.Salary_User_End, firstAend[1]);

                }
                intent.putExtras(bundle);
                /*以上的逻辑从month中跳转到 详情*/
                intent.setClass(getActivity(), DayDetailActivity.class);
                startActivity(intent);
            }
        });
    }

    private void addData() {
        showProgressDialog();
        mPresenter.getAccountPeriodListByDate(setNewMapMessage());
    }

    private String[] initTwoTime(TimeShowMoneyMessage.AccountPeriodListByDateBean byDateBean) {
        String monthFirstEnd[] = new String[2];
        int showType = byDateBean.getShowType();
        //初始化时间
        if (showType == 1) {
            monthFirstEnd = TimeUtils.getYearFirstLastDay(Calendar.getInstance(),
                    TimeUtils.stringTolong(byDateBean.getCreateDate(), TimeUtils.YEAR_MONTH_DAY));
        } else if (showType == 2) {
            try {
                monthFirstEnd = TimeUtils.getNowMonthFirstLastday(Calendar.getInstance(),
                        TimeUtils.stringTolong(byDateBean.getCreateDate(), TimeUtils.YEAR_MONTH_DAY));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else if (showType == 3) {
            monthFirstEnd[0] = TimeUtils.dateToString(TimeUtils.stringToDate(byDateBean.getCreateDate(), TimeUtils.
                    YEAR_MONTH_DAY), TimeUtils.YEAR_MONTH_DAY);
            monthFirstEnd[1] = monthFirstEnd[0];
        }
        return monthFirstEnd;
    }


    private void initAdapter() {
        setRecy(myRecycler);
        listByDateBeen = new ArrayList<TimeShowMoneyMessage.AccountPeriodListByDateBean>();
        timeFragmentAdapter = new TimeFragmentAdapter(R.layout.recycler_io_coard, listByDateBeen
                , bundle.getBoolean(Constant.ShowIncome), bundle.getBoolean(Constant.ShowExpend), context);
        if (null!=timeFragmentAdapter.getEmptyView()){
            timeFragmentAdapter.setEmptyView(emptyView);
        }
        myRecycler.setAdapter(timeFragmentAdapter);
    }


    @Override
    protected void initData() {

    }

    /**
     * @param state     判断是本月 还是上月 还是本年
     * @param beginDate
     * @param endDate
     */
    public void swipeRefreshData(int state, int accid, String beginDate, String endDate) {
        this.state = state;
        this.accid = accid;
        this.beginDate = beginDate;
        this.endDate = endDate;
        addData();
    }

    private Map<String, String> setNewMapMessage() {
        Map<String, String> map = new HashMap<>();
        if (accid != 0) {
            map.put("fid", String.valueOf(accid));
        }
        map.put("oid", String.valueOf(user.getOid()));
        map.put("state", String.valueOf(state));
        if (!TextUtils.isEmpty(beginDate) && !TextUtils.isEmpty(endDate)) {
            map.put("monthOrDay", "" + (TimeUtils.isOneYear(beginDate, endDate) ?
                    (TimeUtils.isOneMonth(beginDate,
                            endDate) ? 2 : 1) : 1));
            map.put("beginDate", beginDate);
            map.put("endDate", endDate);
        }
        return map;

    }

    @Override
    public void setBean(TimeShowMoneyMessage body) {
        dissProgressDialog();
        listByDateBeen.clear();
        listByDateBeen.addAll(body.getAccountPeriodListByDate());
        timeFragmentAdapter.setSatate(state, beginDate, endDate);
        timeFragmentAdapter.setNewData(listByDateBeen);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void setOnResume(boolean isOnResum) {
        this.isOnResum = isOnResum;
    }
}
